/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package filesecurity;

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author lehoangkhanh
 */
public class ExportKey extends javax.swing.JInternalFrame {

    /**
     * Creates new form ExportKey
     */
    String fileName = null;
    String filePath = null;
    String folderPath = null;
    MainFrame rootFrame = null;
    public ExportKey(MainFrame root) {
        initComponents();
        rootFrame = root;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        savein = new javax.swing.JToggleButton();
        jLabel1 = new javax.swing.JLabel();
        jToggleButton2 = new javax.swing.JToggleButton();

        setClosable(true);
        setIconifiable(true);
        setTitle("Export Key Pair");

        savein.setText("...");
        savein.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                saveinMouseClicked(evt);
            }
        });

        jLabel1.setText("Save in:");

        jToggleButton2.setText("OK<<");
        jToggleButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel1)
                .addGap(30, 30, 30)
                .addComponent(savein, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(119, Short.MAX_VALUE)
                .addComponent(jToggleButton2)
                .addGap(97, 97, 97))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(savein))
                .addGap(15, 15, 15)
                .addComponent(jToggleButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void saveinMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_saveinMouseClicked
        // TODO add your handling code here:
        JFileChooser file = new JFileChooser();
        int op = file.showSaveDialog(null);
        if(op == JFileChooser.APPROVE_OPTION){
            filePath = file.getCurrentDirectory().getPath().toString();
            fileName = file.getSelectedFile().getName();
            savein.setText("Just Selected...");
            savein.setToolTipText(filePath);
        }
    }//GEN-LAST:event_saveinMouseClicked

    private void jToggleButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton2ActionPerformed
        // TODO add your handling code here:
        User currentUser = rootFrame.getCurrentUser();
        if(currentUser == null){
            JOptionPane.showMessageDialog(rootPane, "No user to export!!!");
            return;
        }
        Utils.exportUser(currentUser, filePath+"/"+fileName);
        JOptionPane.showMessageDialog(rootPane, "Export user successful!!!");
        this.setVisible(false);
    }//GEN-LAST:event_jToggleButton2ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JToggleButton jToggleButton2;
    private javax.swing.JToggleButton savein;
    // End of variables declaration//GEN-END:variables
}
